


let data = {
    pagenum: 1,
    pagesize: 3
}
//
function shuaxinyemian() {
    $.ajax({
        type: "GET",
        url: "/admin/users",
        data,
        success: function (res) {
            let str = ''
            res.data.forEach((item) => {
                str += `<tr>
        <td>${item.id}</td>
        <td>${item.username}</td>
        <td>${item.nickname}</td>
        <td> ${item.email}</td>
        <td><a href="./edit.html?id=${item.id}" class="layui-btn" id="bianji">编辑</a> <a href="javascript:;"
            class="layui-btn layui-btn-danger shanchu" my-id="${item.id}">删除</a> <a href="javascript:;" class="layui-btn layui-btn-normal chongzhi" my-id=${item.id}>
            重置密码</a></td>
      </tr>`
            })
            $("tbody").html(str)
            let total = res.total
            layui.use('laypage', function () {
                var laypage = layui.laypage;

                //执行一个laypage实例
                laypage.render({
                    elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
                    , count: total,//数据总数，从服务端得到
                    layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
                    groups: 2,
                    curr: data.pagenum,
                    limit: data.pagesize,
                    limits: [3, 10, 20, 30, 40, 50],
                    jump: function (obj, first) {
                        if (!first) {
                            data.pagesize = obj.limit
                            data.pagenum = obj.curr;
                            shuaxinyemian()
                        }
                    }
                });
            });
        }
    })
}
shuaxinyemian()


//点击删除按钮
$("tbody").on('click', ".shanchu", function (e) {
    let id = $(this).attr("my-id")
    // console.log(id);
    layer.confirm('确认要删除用户吗？', function (index) {
        $.ajax({
            type: "DELETE",
            url: "/admin/users/" + id,
            success: function (res) {
                //console.log(res);
                layer.msg(res.message)
            }
        })
        shuaxinyemian()
    })

})




let add_str = `<form class="layui-form add-form" action="" style="margin: 30px; margin-left: 0px;" id="add_form">
                <div class="layui-form-item">
                  <label class="layui-form-label">密码</label>
                  <div class="layui-input-block">
                    <input type="password" name="password" required lay-verify="required|changdu" placeholder="请输入密码" autocomplete="off" class="layui-input">
                  </div>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">确认密码</label>
                  <div class="layui-input-block">
                    <input type="password" name="password" required lay-verify="required|changdu|same" placeholder="请输入新密码" id="pwd" autocomplete="off" class="layui-input">
                  </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                  </div>
                </div>
               </form>`;


let form = layui.form
//表单验证
form.verify({
    changdu: function (value, item) {
        let reg = /^\S{6,12}$/;
        if (reg.test(value) == false) {
            return "密码不符合要求"
        }
    },
    same: function (value, item) {
        // value:重新输入密码值
        // 第一次输入的密码：item？$("#pwd").val();
        if (value != $("#pwd").val()) {
            return "两次密码输入不一致"
        }
    }


    // changdu1: [/^\S{6,12}$/, "密码不符合要求"]
});

//点击重置密码
$("tbody").on("click", ".chongzhi", function (e) {
    e.preventDefault()
    let id = $(this).attr("my-id")
    // 2.弹窗:核心方法，仔细读文档
    let index = layer.open({
        type: 1,
        content: add_str,
        title: '重置密码',
        area: ['500px', '250px'],
    });

    $(".add-form").on("submit", function (e) {
        e.preventDefault();
        $.ajax({
            url: "/admin/users/" + id,
            type: "PUT",
            data: {
                password: $(this).serialize()
            },
            success: function (res) {
                console.log(res);
                layer.msg(res.message);
                if (res.status == 0) {
                    layer.close(index);
                }
            }
        })
    });

});
